<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="档案名称" prop="archiveName">
        <el-input
          v-model="queryParams.archiveName"
          placeholder="请输入档案名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label-width="120px" label="变更申请人名称" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入变更申请人的名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label-width="130px"
        label="变更申请提交时间"
        prop="changeApplyTime"
      >
        <el-date-picker
          clearable
          v-model="queryParams.changeApplyTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择变更申请提交的时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="审批状态" prop="shenpiState">
        <el-select
          v-model="queryParams.shenpiState"
          placeholder="请选择审批状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.apply_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-if="menu === 'arch'"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:change:add']"
          >申请档案变更</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:change:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:change:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="changeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="档案变更申请的编号" align="center" prop="id" />
      <el-table-column label="档案名称" align="center" prop="archiveName" />
      <el-table-column label="档案编号" align="center" prop="archiveId" />
      <el-table-column
        label="档案变更申请人的名称"
        align="center"
        prop="username"
      />
      <el-table-column
        label="档案变更申请人编号"
        align="center"
        prop="userId"
      />
      <el-table-column
        label="档案变更申请的原因"
        align="center"
        prop="changeExplain"
      />
      <el-table-column
        label="变更后的档案简述"
        align="center"
        prop="changeArchiveContent"
      />
      <template>
        <el-table-column
          label="变更文件"
          align="center"
          prop="filePath"
          min-width="300px"
          show-overflow-tooltip
          fixed="right"
        >
          <template slot-scope="scope">
            <el-tag
              type="info"
              v-if="scope.row.filePath == null || scope.row.filePath === ''"
              >无文件</el-tag
            >
            <div v-if="scope.row.filePath != null && scope.row.filePath !== ''">
              <li
                :key="path"
                class="el-upload-list__item ele-upload-list__item-content"
                v-for="path in getFilePath(scope.row.filePath)"
              >
                <el-link :href="path" :underline="false" target="_blank">
                  <span class="el-icon-document">
                    {{ getFileName(path) }}
                  </span>
                </el-link>
              </li>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="变更图片"
          align="center"
          prop="imagePath"
          width="100"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-tag
              type="info"
              v-if="scope.row.imagePath == null || scope.row.imagePath === ''"
              >无图片</el-tag
            >
            <image-preview
              v-if="scope.row.imagePath != null && scope.row.imagePath !== ''"
              :src="scope.row.imagePath"
              :width="50"
              :height="50"
            />
          </template>
        </el-table-column>
      </template>
      <el-table-column
        label="档案变更申请提交时间"
        align="center"
        prop="changeApplyTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.changeApplyTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审批状态" align="center" prop="shenpiState">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.apply_status"
            :value="scope.row.shenpiState"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="企业级档案管理员的名称"
        align="center"
        prop="enterpriseAdminName"
      />
      <el-table-column
        label="企业级档案管理员的编号"
        align="center"
        prop="enterpriseAdminId"
      />
      <el-table-column
        label="企业级档案管理员的审批意见"
        align="center"
        prop="enterpriseAdminOpinion"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.approval_opinion"
            :value="scope.row.enterpriseAdminOpinion"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="企业级档案管理员的备注说明"
        align="center"
        prop="enterpriseAdminNote"
      />
      <el-table-column
        label="政府级档案管理员的名称"
        align="center"
        prop="govermentAdminName"
      />
      <el-table-column
        label="政府级档案管理员的编号"
        align="center"
        prop="govermentAdminId"
      />
      <el-table-column
        label="政府级档案管理员的审批意见"
        align="center"
        prop="govermentAdminOpinion"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.approval_opinion"
            :value="scope.row.govermentAdminOpinion"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="政府级档案管理员的备注说明"
        align="center"
        prop="govermentAdminNote"
      />
      <el-table-column
        width="200px"
        fixed="right"
        v-if="menu !== 'arch'"
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:change:edit']"
          >修改</el-button> -->
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            v-if="menu === 'en'"
            @click="handleUpdateEnterprise(scope.row)"
            v-hasPermi="['system:change:edit']"
            >企业审批</el-button
          >

          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            v-if="menu === 'gv'"
            @click="handleUpdateGovernment(scope.row)"
            v-hasPermi="['system:change:edit']"
            >政府审批</el-button
          >

          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:change:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改档案变更申请对话框 -->
    <el-dialog
      v-if="title === '申请档案变更'"
      :visible.sync="open"
      width="800px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="160px">
        <!-- <el-form-item label="档案编号" prop="archiveId">
          <el-input v-model="form.archiveId" placeholder="请输入档案id" />
        </el-form-item>
        <el-form-item label="档案名称" prop="archiveName">
          <el-input v-model="form.archiveName" placeholder="请输入档案名称" />
        </el-form-item> -->
        <el-form-item label="档案名称" prop="archiveId">
          <el-select
            v-model="form.archiveId"
            filterable
            clearable
            placeholder="请选择"
          >
            <el-option
              v-for="item in arOptions"
              :key="item.id"
              :label="item.archiveName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="档案变更申请的原因" prop="changeExplain">
          <el-input
            v-model="form.changeExplain"
            placeholder="请输入档案变更申请的原因"
          />
        </el-form-item>
        <el-form-item label="变更后的档案简述">
          <editor v-model="form.changeArchiveContent" :min-height="192" />
        </el-form-item>
        <!-- <el-form-item label="变更后的档案文件地址" prop="changeArchiveAddress">
          <el-input v-model="form.changeArchiveAddress" placeholder="请输入变更后的档案文件地址" />
        </el-form-item> -->
        <template>
          <el-form-item label="变更图片上传" prop="imagePath">
            <image-upload v-model="form.imagePath" :fileType="imgType" />
          </el-form-item>
          <el-form-item label="变更文件上传" prop="filePath">
            <file-upload v-model="form.filePath" />
          </el-form-item>
        </template>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 档案变更企业审批对话框 -->
    <el-dialog
      v-if="title === '档案变更企业审批'"
      :visible.sync="open"
      width="800px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="160px">
        <el-form-item
          label="企业级档案管理员审批意见"
          prop="enterpriseAdminOpinion"
        >
          <el-select
            v-model="form.enterpriseAdminOpinion"
            placeholder="请选择审批意见"
            clearable
          >
            <el-option
              v-for="dict in dict.type.approval_opinion"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="企业级档案管理员备注说明"
          prop="enterpriseAdminNote"
        >
          <el-input
            v-model="form.enterpriseAdminNote"
            placeholder="请输入档备注说明"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 档案借阅政府审批对话框 -->
    <el-dialog
      v-if="title === '档案变更政府审批'"
      :visible.sync="open"
      width="800px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="160px">
        <el-form-item
          label="政府级档案管理员审批意见"
          prop="govermentAdminOpinion"
        >
          <el-select
            v-model="form.govermentAdminOpinion"
            placeholder="请选择审批意见"
            clearable
          >
            <el-option
              v-for="dict in dict.type.approval_opinion"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="政府级档案管理员备注说明"
          prop="govermentAdminNote"
        >
          <el-input
            v-model="form.govermentAdminNote"
            placeholder="请输入备注说明"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listChange,
  getChange,
  delChange,
  addChange,
  updateChange,
} from "@/api/system/change";
import {
  listDArchive,
  getDArchive,
  delDArchive,
  addDArchive,
  updateDArchive,
} from "@/api/system/DArchive";
import { getInfo } from "@/api/login";

export default {
  name: "Change",
  dicts: ["apply_status", "approval_opinion"],
  data() {
    return {
      //上传图片类型
      imgType: ["png", "jpg", "jpeg"],
      // 项目根路径
      baseUrl: process.env.VUE_APP_BASE_API,
      file: null,
      menu: null,
      user: null,
      arOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 档案变更申请表格数据
      changeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        archiveId: null,
        archiveName: null,
        userId: null,
        username: null,
        changeExplain: null,
        changeArchiveContent: null,
        changeArchiveAddress: null,
        changeApplyTime: null,
        shenpiState: null,
        enterpriseAdminId: null,
        enterpriseAdminOpinion: null,
        enterpriseAdminNote: null,
        govermentAdminId: null,
        govermentAdminOpinion: null,
        govermentAdminNote: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        archiveId: [
          { required: true, message: "档案编号不能为空", trigger: "blur" },
        ],
        archiveName: [
          { required: true, message: "档案名称不能为空", trigger: "blur" },
        ],
        userId: [
          {
            required: true,
            message: "档案变更申请人的编号不能为空",
            trigger: "blur",
          },
        ],
        username: [
          {
            required: true,
            message: "档案变更申请人的名称不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getArchList();
    getInfo().then((res) => {
      this.user = res.user;
    });
    // 获取菜单信息
    this.menu = this.$route.query.menu;
  },
  methods: {
    /** 获取档案下拉菜单 */
    getArchList() {
      listDArchive({ archStatus: 0, pageNum: 1, pageSize: 100 }).then(
        (response) => {
          this.arOptions = response.rows;
        }
      );
    },
    /** 查询档案变更申请列表 */
    getList() {
      this.loading = true;
      listChange(this.queryParams).then((response) => {
        this.changeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        archiveId: null,
        archiveName: null,
        userId: null,
        username: null,
        changeExplain: null,
        changeArchiveContent: null,
        changeArchiveAddress: null,
        changeApplyTime: null,
        shenpiState: null,
        enterpriseAdminId: null,
        enterpriseAdminOpinion: null,
        enterpriseAdminNote: null,
        govermentAdminId: null,
        govermentAdminOpinion: null,
        govermentAdminNote: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "申请档案变更";
    },
    /** 企业审批按钮操作 */
    handleUpdateEnterprise(row) {
      if (row.shenpiState == 0 && row.enterpriseAdminOpinion == 0) {
        this.reset();
        this.open = true;
        this.open = true;
        const id = row.id || this.ids;
        getChange(id).then((response) => {
          this.form = response.data;
          this.title = "档案变更企业审批";
        });
      } else {
        this.$modal.msgError("非待审批状态！");
      }
    },
    handleUpdateGovernment(row) {
      if (row.enterpriseAdminOpinion == 1 && row.govermentAdminOpinion == 0) {
        this.reset();
        this.open = true;
        const id = row.id || this.ids;
        getChange(id).then((response) => {
          this.form = response.data;
          this.title = "档案变更政府审批";
        });
      } else {
        this.$modal.msgError("非待审批状态！");
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            this.form.shenpiState = this.form.govermentAdminOpinion;
            updateChange(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addChange(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除档案变更申请编号为"' + ids + '"的数据项？')
        .then(function () {
          return delChange(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/change/export",
        {
          ...this.queryParams,
        },
        `change_${new Date().getTime()}.xlsx`
      );
    },

    // 文件路径处理
    getFilePath(filePaths) {
      var arrayPath = filePaths.split(",");
      for (let i in arrayPath) {
        arrayPath[i] = this.baseUrl + arrayPath[i];
      }
      return arrayPath;
    },

    // 获取文件名称
    getFileName(name) {
      if (name.lastIndexOf("/") > -1) {
        return name.slice(name.lastIndexOf("/") + 1).toLowerCase();
      } else {
        return "";
      }
    },
  },
};
</script>
